package com.huaxin.hxcloud.activity;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.baidu.mapapi.model.LatLng;
import com.emmp_ard.scancapture.ScanCaptureActivity;
import com.google.zxing.WriterException;
import com.huaxin.common.AppManager;
import com.huaxin.common.BaseActivity;
import com.huaxin.common.BaseApp;
import com.huaxin.common.Constants;
import com.huaxin.common.Entity.AroundItemDTO;
import com.huaxin.common.Entity.BaseDTO;
import com.huaxin.common.Entity.CheckPoint;
import com.huaxin.common.Entity.CheckTaskDetailDTO;
import com.huaxin.common.Entity.WaterIntakeDTO;
import com.huaxin.common.Entity.WorkTask;
import com.huaxin.common.media.ImageGalleryActivity;
import com.huaxin.common.tools.HxTransmitRequestHelper;
import com.huaxin.common.tools.Log;
import com.huaxin.common.tools.RequestHelper;
import com.huaxin.common.tts.Constant;
import com.huaxin.common.utils.DialogUtil;
import com.huaxin.common.utils.QrCodeUtils;
import com.huaxin.common.utils.ToastUtil;
import com.huaxin.common.utils.Util;
import com.huaxin.hxcloud.R;
import com.huaxindata.im.main.manager.UserManager;
import com.luck.picture.lib.PictureSelector;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


/**
 * Created by Administrator on 2016/9/28.
 */
public class CheckTaskDetailActivity extends BaseActivity {

    public static final int WORK_TASK_STATE_END = 0x02;

    ArrayList datas = new ArrayList<>();
    @BindView(R.id.tv_station_name)
    TextView          tvStationName;
    @BindView(R.id.tv_title)
    TextView          tvTitle;
    @BindView(R.id.more)
    TextView          more;
    @BindView(R.id.toolbar)
    Toolbar           toolbar;
    @BindView(R.id.check_list)
    RecyclerView      checkList;
    @BindView(R.id.content)
    FrameLayout       content;
    @BindView(R.id.cl)
    CoordinatorLayout cl;

    private MultipleAdapter        multipleAdapter;
    private int                    _workTaskId;
    private String                 _stationCode;
    private int                    _taskType;
    private String                 _title;
    private int                    ret_pos;
    private HxSationDialogFragment mDialogFrament;
    private final static int type_default     = 0x00;
    private final static int type_around_item = 0x01;
    private final static int type_intake      = 0x02;
    private final static int type_check_point = 0x03;
    private final static int type_title       = 0x04;
    private int                 _workTaskState;
    private String              _stationName;
    private List<AroundItemDTO> mCustomAroundBeans;
    private List<CheckPoint>    mCustomCheckPointBeans;
    private PopupWindow         tipPop;
    private TextView            tv_stata;
    private TextView            tv_know;
    private int                 state;


    public static void start(Activity context, WorkTask workTask, int state) {
        Intent intent = new Intent(context, CheckTaskDetailActivity.class);
        intent.putExtra(Constants.IK_WT_ID, workTask.getId());
        intent.putExtra(Constants.IK_STATION_CODE, workTask.getStationCode());
        intent.putExtra(Constants.IK_STATION_NAME, workTask.getStationName());
        intent.putExtra(Constants.IK_WT_TYPE, workTask.getTaskType());
        intent.putExtra(Constants.IK_WT_TITLE, workTask.getTitle());
        intent.putExtra(Constants.IK_WT_STATE, workTask.getWorkTaskState());
        intent.putExtra(Constants.IK_ARRANGE_STATE, state);//判断工单是否完成
        context.startActivityForResult(intent, Constants.RET_WT_STATE);
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        super.getBundleExtras(extras);
        if (extras.containsKey(Constants.IK_WT_ID)) {
            _workTaskId = extras.getInt(Constants.IK_WT_ID);
            _stationCode = extras.getString(Constants.IK_STATION_CODE);
            _stationName = extras.getString(Constants.IK_STATION_NAME);
            _taskType = extras.getInt(Constants.IK_WT_TYPE);
            _title = extras.getString(Constants.IK_WT_TITLE);
            _workTaskState = extras.getInt(Constants.IK_WT_STATE);
            state = extras.getInt(Constants.IK_ARRANGE_STATE);

        } else {
            finish();
        }
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_checktask_detail;
    }

    @Override
    protected int getLoadFrameId() {
        return R.id.content;
    }

    @Override
    public void requestData() {
        ApiRequestData(LOADING);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (RESULT_OK == resultCode) {
            if (Constants.RET_SWEEP == requestCode) {
                WaterIntakeDTO waterIntake = data.getParcelableExtra(Constants.IK_WATER_INTAKE);
                if (datas.get(ret_pos) instanceof WaterIntakeDTO) {
                    ((WaterIntakeDTO) datas.get(ret_pos)).setQrCode(waterIntake.getQrCode());
                    ((WaterIntakeDTO) datas.get(ret_pos)).setIsFinish(waterIntake.getIsFinish());
                    waterIntake = (WaterIntakeDTO) datas.get(ret_pos);
                    datas.remove(ret_pos);
                    datas.add(ret_pos, waterIntake);
                    multipleAdapter.notifyItemChanged(ret_pos);
                }
            }
        }

    }

    private void ApiRequestData(int status) {
        updateLoadStatus(status);
        Call<CheckTaskDetailDTO> call = HxTransmitRequestHelper.getRequestApi().getCheckTaskDetail(_workTaskId,
                _stationCode, _taskType);
        autoClose(call);
        call.enqueue(new Callback<CheckTaskDetailDTO>() {
            @Override
            public void onResponse(Call<CheckTaskDetailDTO> call, Response<CheckTaskDetailDTO>
                    response) {
                CheckTaskDetailDTO checkTaskDetail = response.body();
                if (response.isSuccessful()) {
                    if (checkTaskDetail != null && checkTaskDetail.isSuccessful()) {
                        updateLoadStatus(LOAD_SUCCESS);
                        updateView(checkTaskDetail);
                        CheckTaskDetailDTO.Data data = checkTaskDetail.getData();
                        //巡检项
                        mCustomAroundBeans = data.getCustomAroundItems();
                        //点检项
                        mCustomCheckPointBeans = data.getCustomCheckPoints();
                        //取水口
                        List<WaterIntakeDTO> customWaterInLets = data.getCustomWaterInLets();
                        //spt 未完成工单 需要签到
                        if (Constant.SPTOrgcodeFirst.equals(UserManager.getFirstOrgCode()) && state != WORK_TASK_STATE_END) {
                            if (checkTaskDetail.getIsSignIn() == 0) {
                                apiAddSign(checkTaskDetail.getXunJianStationId());
                            }
                        }
                    } else {
                        updateLoadStatus(LOAD_ERROR);
                    }
                } else {
                    updateLoadStatus(LOAD_ERROR);
                }
            }

            @Override
            public void onFailure(Call<CheckTaskDetailDTO> call, Throwable t) {
                updateLoadStatus(LOAD_FAILURE);
            }
        });
    }

    /**
     * 签到
     *
     * @param xunJianStationId
     */
    private void apiAddSign(int xunJianStationId) {
        LatLng latLng = BaseApp.getLatLng();
        Call<BaseDTO> call = HxTransmitRequestHelper.getRequestApi().addSign(null, new Date().getTime(),
                _stationCode, latLng.longitude, latLng.latitude, null, xunJianStationId);
        autoClose(call);
        call.enqueue(new Callback<BaseDTO>() {
            @Override
            public void onResponse(Call<BaseDTO> call, Response<BaseDTO>
                    response) {
                BaseDTO baseDTO = response.body();
                if (response.isSuccessful()) {
                    if (baseDTO != null) {
                        String msg = baseDTO.getMsg();
                        if (msg != null) {
                            if (msg.contains("失败")) {
                                //                                ToastUtil.showToast(baseDTO.getMsg());
                                //                                finish();
                                signSuccess(baseDTO.getMsg());
                                return;
                            }
                        }
                        ToastUtil.showToast(baseDTO.getMsg());
                    } else {
                        signSuccess("签到失败");
                    }
                } else {
                    if (baseDTO != null) {
                        // ToastUtil.showToast(baseDTO.getMsg());
                        signSuccess(baseDTO.getMsg());
                    } else {
                        // ToastUtil.showToast("数据异常，签到失败");
                        signSuccess("数据异常，签到失败");
                    }
                    // finish();
                }
            }

            @Override
            public void onFailure(Call<BaseDTO> call, Throwable t) {
                // ToastUtil.showToast("网络故障，签到失败");
                signSuccess("网络故障，签到失败");
                // finish();
            }
        });
    }

    /**
     * 打卡提示
     *
     * @param des
     */
    private void signSuccess(String des) {
        //  View parent = ((ViewGroup) findViewById(R.id.content)).getChildAt(0);
        if (tipPop == null) {
            View popView = LayoutInflater.from(this).inflate(R.layout.pop_sign_tip, null);
            tv_stata = (TextView) popView.findViewById(R.id.tv_state);
            tv_know = (TextView) popView.findViewById(R.id.tv_know);
            tipPop = new PopupWindow(popView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        }
        //    tipPop.setOutsideTouchable(false);
        //        tipPop.setFocusable(true);
        //    tipPop.setBackgroundDrawable(null);
        if (!tipPop.isShowing()) {
            tipPop.showAtLocation(cl, Gravity.CENTER, 0, 0);
        }
        tv_stata.setText(des);

        tv_know.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (tipPop.isShowing()) {
                    tipPop.dismiss();
                }
                finish();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //防止leaked
        if (tipPop != null && tipPop.isShowing()) {
            tipPop.dismiss();
        }
    }

    public void updateView(CheckTaskDetailDTO checkTaskDetail) {
        loadText(tvTitle, _title, "工单未填标题");
        datas.clear();
        if (!checkTaskDetail.getData().getCustomAroundItems().isEmpty()) {
            datas.add("站点巡检");
            datas.addAll(checkTaskDetail.getData().getCustomAroundItems());
        }
        if (!checkTaskDetail.getData().getCustomWaterInLets().isEmpty()) {
            //            datas.add("站点取水");
            datas.addAll(checkTaskDetail.getData().getCustomWaterInLets());
        }

        if (!checkTaskDetail.getData().getCustomCheckPoints().isEmpty()) {
            datas.add("设备点检");
            datas.addAll(checkTaskDetail.getData().getCustomCheckPoints());
        }
        multipleAdapter.notifyDataSetChanged();
    }


    @Override
    public void initView() {
        setSupportActionBar(toolbar);
        toolbar.setTitle("巡检单");
        more.setText("提交");
        more.setVisibility(_workTaskState == 3 ? View.GONE : View.VISIBLE);
        tvStationName.setText(_stationName);
        multipleAdapter = new MultipleAdapter();
        checkList.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL,
                false));
        checkList.setAdapter(multipleAdapter);
    }

    @OnClick(R.id.more)
    public void onClick() {
        //ApiCommitXunjian();
        putCommitXunJian();
    }

    /**
     * 提交巡检项
     */
    private void putCommitXunJian() {
        final Dialog dialog = DialogUtil.getWaitDialog(this, "提交中");
        dialog.show();
        //巡检项
        JSONArray aroundItemJsonArray = new JSONArray();
        //点检项
        JSONArray checkPointJasonArray = new JSONArray();
        try {
            if (mCustomAroundBeans != null) {
                for (AroundItemDTO customAroundBean : mCustomAroundBeans) {
                    JSONObject object = new JSONObject();
                    object.put("checkState", customAroundBean.getCheckState());
                    object.put("description", customAroundBean.getDescription());
                    object.put("id", customAroundBean.getId());

                    object.put("imageName", customAroundBean.getImageName());

                    object.put("isFinish", customAroundBean.getIsFinish());
                    //  object.put("itemType", customAroundBean.);
                    object.put("name", customAroundBean.getName());
                    object.put("remark", customAroundBean.getRemark());
                    object.put("stationCode", customAroundBean.getStationCode());

                    aroundItemJsonArray.put(object);
                }
            }
            if (mCustomCheckPointBeans != null) {
                for (CheckPoint customCheckPoinBean : mCustomCheckPointBeans) {
                    JSONObject object = new JSONObject();
                    object.put("checkState", customCheckPoinBean.getCheckState());
                    object.put("description", customCheckPoinBean.getDescription());
                    object.put("id", customCheckPoinBean.getId());

                    object.put("imageName", customCheckPoinBean.getImageName());

                    object.put("isFinish", customCheckPoinBean.getIsFinish());
                    //  object.put("itemType", customAroundBean.);
                    object.put("name", customCheckPoinBean.getName());
                    object.put("remark", customCheckPoinBean.getRemark());
                    object.put("stationCode", customCheckPoinBean.getStationCode());

                    checkPointJasonArray.put(object);

                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        String aroundData = aroundItemJsonArray.toString();
        String checkPointData = checkPointJasonArray.toString();
        Call<BaseDTO> call = HxTransmitRequestHelper.getRequestApi().commitXunJianRadio(_workTaskId, _stationCode, aroundData, checkPointData);
        autoClose(call);
        call.enqueue(new Callback<BaseDTO>() {
            @Override
            public void onResponse(Call<BaseDTO> call, Response<BaseDTO> response) {
                BaseDTO baseDTO = response.body();
                dialog.dismiss();
                if (baseDTO.isSuccessful()) {
                    if (baseDTO != null) {
                        ToastUtil.showToast(baseDTO.getMsg());
                        _workTaskState = 3;
                        setResult();
                        more.setVisibility(View.GONE);
                        AppManager.getAppManager().finishActivity(CheckTaskDetailActivity.this);
                        finish();
                    } else {
                        ToastUtil.showToast("数据异常");
                    }
                } else {
                    if (baseDTO != null) {
                        ToastUtil.showToast(baseDTO.getMsg());
                    } else {
                        ToastUtil.showToast("数据异常");
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseDTO> call, Throwable t) {
                ToastUtil.showToast("网络故障");
            }
        });
    }

    //    @Deprecated
    //    private void ApiCommitXunjian() {
    //        final Dialog dialog = DialogUtil.getWaitDialog(this, "提交中");
    //        dialog.show();
    //        Call<BaseDTO> call = HxTransmitRequestHelper.getRequestApi().commitXunJian(_workTaskId, _stationCode);
    //        autoClose(call);
    //        call.enqueue(new Callback<BaseDTO>() {
    //            @Override
    //            public void onResponse(Call<BaseDTO> call, Response<BaseDTO> response) {
    //                BaseDTO baseDTO = response.body();
    //                dialog.dismiss();
    //                if (response.isSuccessful()) {
    //                    if (baseDTO != null && baseDTO.isSuccessful()) {
    //                        ToastUtil.showToast(baseDTO.getMsg());
    //                        _workTaskState = 3;
    //                        setResult();
    //                        more.setVisibility(View.GONE);
    //
    //                    } else {
    //                        ToastUtil.showToast(baseDTO.getMsg());
    //                    }
    //                } else {
    //                    ToastUtil.showToast("数据异常");
    //                }
    //            }
    //
    //            @Override
    //            public void onFailure(Call<BaseDTO> call, Throwable t) {
    //
    //            }
    //        });
    //    }


    private void setResult() {
        Intent intent = new Intent();
        intent.putExtra(Constants.IK_WT_STATE, _workTaskState);
        setResult(RESULT_OK, intent);
    }


    public class MultipleAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            RecyclerView.ViewHolder holder = null;
            switch (viewType) {
                case type_around_item:
                    holder = new AIViewHolder(mInflater.inflate(R.layout.item_wt_check_point,
                            parent, false));
                    break;
                case type_check_point:
                    holder = new CPViewHolder(mInflater.inflate(R.layout.item_wt_check_point,
                            parent, false));
                    break;
                case type_intake:
                    holder = new WIViewHolder(mInflater.inflate(R.layout.item_wt_intake, parent,
                            false));
                    break;
                case type_title:
                    holder = new TTViewHolder(mInflater.inflate(R.layout.item_title, parent,
                            false));
                default:
                    break;
            }
            return holder;
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            //            Log.d("Zoes", "pos" + position);
            switch (getItemViewType(position)) {
                case type_around_item:
                    ((AIViewHolder) holder).update(position);
                    break;
                case type_check_point:
                    ((CPViewHolder) holder).update(position);
                    break;
                case type_intake:
                    ((WIViewHolder) holder).update(position);
                    break;
                case type_title:
                    ((TTViewHolder) holder).update(position);
                    break;
                default:
                    break;
            }

        }

        @Override
        public int getItemCount() {
            return datas == null ? 0 : datas.size();
        }

        @Override
        public int getItemViewType(int position) {
            if (datas.get(position) instanceof AroundItemDTO) {
                return type_around_item;
            } else if (datas.get(position) instanceof WaterIntakeDTO) {
                return type_intake;
            } else if (datas.get(position) instanceof CheckPoint) {
                return type_check_point;
            } else if (datas.get(position) instanceof String) {
                return type_title;
            }
            return type_default;
        }


        public class CPViewHolder extends RecyclerView.ViewHolder implements View
                .OnClickListener, RadioGroup.OnCheckedChangeListener {
            @BindView(R.id.tv_name)
            TextView     tvName;
            @BindView(R.id.iv_state)
            ImageView    ivState;
            @BindView(R.id.tv_des)
            TextView     tvDes;
            @BindView(R.id.normal)
            RadioButton  normal;
            @BindView(R.id.error)
            RadioButton  error;
            @BindView(R.id.failure)
            RadioButton  failure;
            @BindView(R.id.group)
            RadioGroup   group;
            @BindView(R.id.bt_add_des)
            ImageButton  btAddDes;
            @BindView(R.id.ll_radio)
            LinearLayout llRadio;
            @BindView(R.id.tv_remark)
            TextView     tvRemark;
            @BindView(R.id.ls_img)
            RecyclerView lsImg;
            ImgAdapter adapter;
            int        position;

            public CPViewHolder(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
                adapter = new ImgAdapter();
                lsImg.setLayoutManager(
                        new LinearLayoutManager(CheckTaskDetailActivity.this, LinearLayoutManager.HORIZONTAL, false));
                lsImg.setAdapter(adapter);
            }

            public void update(int position) {
                this.position = position;

                CheckPoint data = (CheckPoint) datas.get(position);

                loadText(tvName, data.getName());
                loadText(tvDes, data.getDescription());
                group.setOnCheckedChangeListener(this);
                btAddDes.setOnClickListener(this);
                if (data.getIsFinish() == 0) {
                    llRadio.setVisibility(View.VISIBLE);
                    tvRemark.setVisibility(View.GONE);
                    ivState.setVisibility(View.GONE);
                } else {
                    llRadio.setVisibility(View.GONE);
                    tvRemark.setVisibility(View.VISIBLE);
                    ivState.setVisibility(View.VISIBLE);
                    loadText("结果：", tvRemark, data.getRemark(), "无描述");
                    loadState(ivState, data.getCheckState());
                    adapter.setDatas(data.getImageName());
                    adapter.notifyDataSetChanged();
                }
            }


            @Override
            public void onClick(View v) {
                final CheckPoint data = (CheckPoint) datas.get(position);
                mDialogFrament = HxSationDialogFragment.newInstance();
                mDialogFrament.show(getFragmentManager(), "video");
                //spt 力鼎上传视频
                Bundle bundle = new Bundle();
                bundle.putBoolean(Constants.DIALOG_IS_VIDEO, true);
                if (Constant.SPTOrgcodeFirst.equals(UserManager.getFirstOrgCode()) || Constant.LDOrgcodeFirst.equals(UserManager.getFirstOrgCode())) {
                    bundle.putBoolean(Constants.DIALOG_IS_VIDEO, true);
                } else {
                    bundle.putBoolean(Constants.DIALOG_IS_VIDEO, false);
                }
                mDialogFrament.setArguments(bundle);
                mDialogFrament.setLoadLisetener(new HxSationDialogFragment.UpLoadLisetener() {
                    @Override
                    public void upload() {
                        ApiCommitCheckPoint(data.getId(), data.getCheckState(), mDialogFrament
                                        .getDes(),
                                mDialogFrament.getImgs());
                    }
                });

            }

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                CheckPoint data = (CheckPoint) datas.get(position);
                switch (checkedId) {
                    case R.id.normal:
                        data.setCheckState(0);
                        break;
                    case R.id.error:
                        data.setCheckState(1);
                        break;
                    case R.id.failure:
                        data.setCheckState(2);
                        break;
                    default:
                        break;
                }
            }

            private void updateData(CheckPoint checkPoint) {
                datas.remove(position);
                datas.add(position, checkPoint);
                multipleAdapter.notifyItemChanged(position);
            }


            private void ApiCommitCheckPoint(Integer id, Integer state, String des, String[] imgs) {
                final ProgressDialog dialog = DialogUtil.getWaitDialog(CheckTaskDetailActivity
                        .this, "提交中...");
                dialog.show();
                Call<CheckPoint> call;
                if (mDialogFrament.isVideo()) {
                    call = HxTransmitRequestHelper.getRequestApi().commitCheckPoint(id, state, des,
                            RequestHelper.createVideoParts(imgs));
                } else {
                    call = HxTransmitRequestHelper.getRequestApi().commitCheckPoint(id, state, des,
                            RequestHelper.createFileParts(imgs));
                }
                autoClose(call);
                call.enqueue(new Callback<CheckPoint>() {
                    @Override
                    public void onResponse(Call<CheckPoint> call, Response<CheckPoint> response) {
                        dialog.dismiss();
                        CheckPoint checkPoint = response.body();
                        if (response.isSuccessful()) {
                            if (checkPoint != null && checkPoint.isSuccessful()) {
                                if (!Util.checkNull(mDialogFrament)) {
                                    mDialogFrament.dismiss();
                                }
                                updateData(checkPoint);
                            } else {
                                ToastUtil.showToast(checkPoint.getMsg());
                            }
                        } else {
                            ToastUtil.showToast(response.message());
                        }
                    }

                    @Override
                    public void onFailure(Call<CheckPoint> call, Throwable t) {
                        ToastUtil.showToast("网络故障");
                    }
                });

            }
        }

        public class AIViewHolder extends RecyclerView.ViewHolder implements View
                .OnClickListener, RadioGroup.OnCheckedChangeListener {
            @BindView(R.id.tv_name)
            TextView     tvName;
            @BindView(R.id.iv_state)
            ImageView    ivState;
            @BindView(R.id.tv_des)
            TextView     tvDes;
            @BindView(R.id.normal)
            RadioButton  normal;
            @BindView(R.id.error)
            RadioButton  error;
            @BindView(R.id.failure)
            RadioButton  failure;
            @BindView(R.id.group)
            RadioGroup   group;
            @BindView(R.id.bt_add_des)
            ImageButton  btAddDes;
            @BindView(R.id.ll_radio)
            LinearLayout llRadio;
            @BindView(R.id.tv_remark)
            TextView     tvRemark;
            @BindView(R.id.ls_img)
            RecyclerView lsImg;
            ImgAdapter adapter;
            int        position;

            public AIViewHolder(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
                adapter = new ImgAdapter();
                lsImg.setLayoutManager(
                        new LinearLayoutManager(CheckTaskDetailActivity.this, LinearLayoutManager.HORIZONTAL, false));
                lsImg.setAdapter(adapter);
            }

            public void update(int position) {
                this.position = position;
                AroundItemDTO data = (AroundItemDTO) datas.get(position);
                loadText(tvName, data.getName());
                loadText(tvDes, data.getDescription());
                group.setOnCheckedChangeListener(this);
                btAddDes.setOnClickListener(this);
                if (data.getIsFinish() == 0) {
                    llRadio.setVisibility(View.VISIBLE);
                    tvRemark.setVisibility(View.GONE);
                    ivState.setVisibility(View.GONE);
                } else {
                    llRadio.setVisibility(View.GONE);
                    tvRemark.setVisibility(View.VISIBLE);
                    ivState.setVisibility(View.VISIBLE);
                    loadText("结果：", tvRemark, data.getRemark(), "无描述");
                    loadState(ivState, data.getCheckState());
                    adapter.setDatas(data.getImageName());
                    adapter.notifyDataSetChanged();
                }
            }


            @Override
            public void onClick(View v) {
                final AroundItemDTO data = (AroundItemDTO) datas.get(position);
                mDialogFrament = HxSationDialogFragment.newInstance();
                mDialogFrament.show(getFragmentManager(), "video");
                Bundle bundle = new Bundle();
                bundle.putBoolean(Constants.DIALOG_IS_VIDEO, true);
                if (Constant.SPTOrgcodeFirst.equals(UserManager.getFirstOrgCode()) || Constant.LDOrgcodeFirst.equals(UserManager.getFirstOrgCode())) {
                    bundle.putBoolean(Constants.DIALOG_IS_VIDEO, true);
                } else {
                    bundle.putBoolean(Constants.DIALOG_IS_VIDEO, false);
                }
                mDialogFrament.setArguments(bundle);
                mDialogFrament.setLoadLisetener(new HxSationDialogFragment.UpLoadLisetener() {
                    @Override
                    public void upload() {
                        ApiCommitAroundItem(data.getId(), data.getCheckState(), mDialogFrament
                                        .getDes(),
                                mDialogFrament.getImgs());
                    }
                });
            }

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                AroundItemDTO data = (AroundItemDTO) datas.get(position);
                switch (checkedId) {
                    case R.id.normal:
                        data.setCheckState(0);
                        break;
                    case R.id.error:
                        data.setCheckState(1);
                        break;
                    case R.id.failure:
                        data.setCheckState(2);
                        break;
                    default:
                        break;
                }
            }

            private void updateData(AroundItemDTO aroundItem) {
                datas.remove(position);
                datas.add(position, aroundItem);
                multipleAdapter.notifyItemChanged(position);
            }


            private void ApiCommitAroundItem(int id, int state, String des, String[] imgs) {
                final ProgressDialog dialog = DialogUtil.getWaitDialog(CheckTaskDetailActivity
                        .this, "提交中...");
                Call<AroundItemDTO> call;
                if (mDialogFrament.isVideo()) {
                    Log.i("lwjing", "视频上传：" + imgs[0]);
                    call = HxTransmitRequestHelper.getRequestApi().commitAroundItem(id, state, des,
                            RequestHelper.createVideoParts(imgs));
                } else {
                    call = HxTransmitRequestHelper.getRequestApi().commitAroundItem(id, state, des,
                            RequestHelper.createFileParts(imgs));
                }

                autoClose(call);
                call.enqueue(new Callback<AroundItemDTO>() {
                    @Override
                    public void onResponse(Call<AroundItemDTO> call, Response<AroundItemDTO>
                            response) {
                        dialog.dismiss();
                        AroundItemDTO aroundItem = response.body();
                        if (response.isSuccessful()) {
                            if (aroundItem != null && aroundItem.isSuccessful()) {
                                if (!Util.checkNull(mDialogFrament)) {
                                    mDialogFrament.dismiss();
                                }
                                updateData(aroundItem);
                            } else {
                                ToastUtil.showToast(aroundItem.getMsg());
                            }
                        } else {
                            ToastUtil.showToast(response.message());
                        }
                    }

                    @Override
                    public void onFailure(Call<AroundItemDTO> call, Throwable t) {
                        ToastUtil.showToast("网络故障");
                    }
                });
            }


        }


        public class WIViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
            @BindView(R.id.tv_name)
            TextView  tvName;
            @BindView(R.id.bt_rq_code)
            Button    btRqCode;
            @BindView(R.id.iv_qr_code)
            ImageView ivQrCode;


            int position;

            public WIViewHolder(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);

            }

            public void update(int position) {
                this.position = position;
                WaterIntakeDTO wiData = (WaterIntakeDTO) datas.get(position);
                loadText(tvName, wiData.getName());
                if (wiData.getIsFinish() == 0) {
                    btRqCode.setVisibility(View.VISIBLE);
                    ivQrCode.setVisibility(View.GONE);
                    btRqCode.setOnClickListener(this);
                } else {
                    btRqCode.setVisibility(View.GONE);
                    ivQrCode.setVisibility(View.VISIBLE);
                    try {
                        ivQrCode.setImageBitmap(QrCodeUtils.CreateBarCode(wiData
                                .getQrCode()));
                    } catch (WriterException e) {
                        e.printStackTrace();
                    }
                }
            }


            @Override
            public void onClick(View v) {
                ret_pos = position;
                WaterIntakeDTO wiData = (WaterIntakeDTO) datas.get(position);
                ScanCaptureActivity.start(CheckTaskDetailActivity.this, ScanCaptureActivity
                        .TYPE_QR_CODE, ScanCaptureActivity
                        .MODE_BIND_WATER, wiData.getId());
            }
        }


        public class TTViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.tv_title)
            TextView tvTitle;

            public TTViewHolder(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
            }

            public void update(int position) {
                String title = (String) datas.get(position);
                loadText(tvTitle, title);
            }
        }
    }

    public class ImgAdapter extends RecyclerView.Adapter<ImgAdapter.ImgHolder> {
        List<String> datas;

        public void setDatas(List<String> datas) {
            this.datas = datas;
        }

        @Override
        public ImgHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new ImgHolder(mInflater.inflate(R.layout.item_h_image, parent, false));
        }

        @Override
        public void onBindViewHolder(ImgHolder holder, int position) {
            holder.update(position);
        }

        @Override
        public int getItemCount() {
            return datas == null ? 0 : datas.size();
        }

        class ImgHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
            @BindView(R.id.iv_img)
            ImageView ivImg;
            @BindView(R.id.play)
            ImageView play;
            View itemView;
            int  position;

            public ImgHolder(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
                this.itemView = itemView;
            }

            public void update(int position) {
                this.position = position;
                Log.i("lwjing", "imagePath=" + addHost(datas.get(position)));
                ViewGroup.LayoutParams params = ivImg.getLayoutParams();
                params.width = Util.getWindowWidth(CheckTaskDetailActivity.this) / 4 - 20;
                params.height = params.width;
                ivImg.setLayoutParams(params);
                loadNetImg(ivImg, addHost(datas.get(position)));
                if (datas.get(position).contains(".mp4")) {
                    play.setVisibility(View.VISIBLE);
                } else {
                    play.setVisibility(View.GONE);
                }
                itemView.setOnClickListener(this);
                play.setOnClickListener(this);
            }


            @Override
            public void onClick(View v) {
                //TODO 视频提前下载
                if (datas.get(position).contains(".mp4")) {
                    ToastUtil.showToast("视频下载中，请稍等...");
                    PictureSelector.create(CheckTaskDetailActivity.this).externalPictureVideo(addHost(datas.get(position)));
                } else {
                    ImageGalleryActivity.show(CheckTaskDetailActivity.this, addHost(getUrls()),
                            position, false);
                }

            }

            private String[] getUrls() {
                return datas.toArray(new String[datas.size()]);
            }
        }
    }

    public void loadState(ImageView imageView, Integer state) {
        switch (state) {
            case 0:
                imageView.setImageResource(R.drawable.icon_state_normal);

                break;
            case 1:
                imageView.setImageResource(R.drawable.icon_state_error);

                break;
            case 2:
                imageView.setImageResource(R.drawable.icon_state_stop);

                break;
            default:
                break;
        }
    }


}